package org.joy.db2;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.util.Vector;

import org.joy.analyzer.Hit;

import com.sleepycat.bind.tuple.TupleInput;
import com.sleepycat.bind.tuple.TupleOutput;
import com.sleepycat.db.DatabaseEntry;
import com.sleepycat.util.FastInputStream;

public class IndexEntry extends Entry {

    private int termID;
    private int URLID;
    private float score;
    private Vector<Integer> pos;

    public IndexEntry() {
    }

    public IndexEntry(int termID, int uRLID, double score, Vector<Integer> pos) {
        super();
        this.termID = termID;
        URLID = uRLID;
        setScore(score);
        this.pos = pos;
    }

    public IndexEntry(String URL, Hit h) {
        this(h.getTerm().hashCode(), URL.hashCode(), h.getScore(), h.getPos());
    }

    public int getTermID() {
        return termID;
    }

    public void setTermID(int termID) {
        this.termID = termID;
    }

    public int getURLID() {
        return URLID;
    }

    public void setURLID(int uRLID) {
        URLID = uRLID;
    }

    public float getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = (float) score;
    }

    public Vector<Integer> getPos() {
        return pos;
    }

    public void setPos(Vector<Integer> pos) {
        this.pos = pos;
    }

    @Override
    public DatabaseEntry getKeyEntry() {
        TupleOutput out = new TupleOutput();
        out.writeInt(termID);
        out.writeInt(URLID);
        return new DatabaseEntry(out.toByteArray());
    }

    @Override
    public DatabaseEntry getValEntry() {
        TupleOutput out = new TupleOutput();
        out.writeFloat(score);
        out.writeInt(pos.size());
        for (int i : pos) {
            out.writeInt(i);
        }
        return new DatabaseEntry(out.toByteArray());
    }

    public void read(DatabaseEntry key, DatabaseEntry val) {

        TupleInput keyIn = new TupleInput(key.getData());
        TupleInput valIn = new TupleInput(val.getData());

        this.termID = keyIn.readInt();
        this.URLID = keyIn.readInt();
        this.score = valIn.readFloat();
        this.pos = new Vector<Integer>();
        int length = valIn.readInt();
        for (int i = 0; i < length; i++) {
            pos.add(valIn.readInt());
        }

    }
}
